{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-10-25T11:39:06.761911Z",
     "start_time": "2024-10-25T11:39:06.759254Z"
    }
   },
   "source": [
    "def customer():\n",
    "    while True:\n",
    "        n = yield \n",
    "        if not n:\n",
    "            return \n",
    "        print(f\"消费者消费了({n})号包子\")\n",
    "\n",
    "# 生产者代码\n",
    "def productor(c):\n",
    "    # 消费者的生成器启动\n",
    "    c.send(None)\n",
    "    for i in range(5):\n",
    "        print(f\"生产者生产了({i + 1})号包子\")\n",
    "        c.send(str(i + 1))\n",
    "\n",
    "c = customer()\n",
    "productor(c)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生产者生产了(1)号包子\n",
      "消费者消费了(1)号包子\n",
      "生产者生产了(2)号包子\n",
      "消费者消费了(2)号包子\n",
      "生产者生产了(3)号包子\n",
      "消费者消费了(3)号包子\n",
      "生产者生产了(4)号包子\n",
      "消费者消费了(4)号包子\n",
      "生产者生产了(5)号包子\n",
      "消费者消费了(5)号包子\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
